package com.common;


import java.sql.SQLException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

@SuppressWarnings("unchecked")
public class DAOUtilImpl extends HibernateDaoSupport  {
	private SessionFactory mySessionFactory;

	@PostConstruct
	protected void initDao() {
		super.setSessionFactory(mySessionFactory);
	}

	public Long getSequenceNextval(final String sequence) throws Exception {
		return ((Long) getHibernateTemplate().execute(new HibernateCallback() {

			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createSQLQuery("select " + sequence
						+ ".nextval from dual");
				Object object = query.uniqueResult();
				return Long.valueOf(Long.parseLong(object.toString()));
			}
		}));
	}

	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory) {
		this.mySessionFactory = sessionFactory;
	}
	
	public void listByPage(final String hql, final PageResult pageResult,final List hqlValues) {
		
		this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session s)
					throws HibernateException, SQLException {
				if (null==hql){
					return null;
				}
				Query query = s.createQuery(hql);
				if(hqlValues!=null){
					for (int i = 0; i < hqlValues.size(); i++) {
						query.setString(i, hqlValues.get(i).toString());
					}
				}
				query.setCacheable(true);
				query.setFirstResult(pageResult.getFirstRec());
				query.setMaxResults(pageResult.getPageSize());		
				List ret = query.list();
				pageResult.setList(ret);
				
				String queryString = "";
				if (hql.toUpperCase().indexOf("SELECT") != -1) {
					int i = query.getQueryString().toUpperCase().indexOf("FROM");
					queryString = "Select count(*) " + hql.substring(i,hql.length());
				} else {
					queryString = "Select count(*) " + hql;
				}
				
				int j = queryString.toUpperCase().lastIndexOf("ORDER");
				if (j!=-1){
					queryString = queryString.substring(0, j);
				}		
				Query cquery = s.createQuery(queryString);
				if(hqlValues!=null){
					for (int i = 0; i < hqlValues.size(); i++) {
						cquery.setString(i, hqlValues.get(i).toString());
					}
				}
				cquery.setCacheable(true);
				//System.out.println(cquery.iterate().next().toString());
				int recTotal =Integer.parseInt(cquery.iterate().next().toString());
				//int recTotal = ((Integer)cquery.iterate().next()).intValue();		
				pageResult.setRecTotal(recTotal);
				return null;
			}
		});
		
		/*if (null==hql){
			return ;
		}
		Query query = this.getSession().createQuery(hql);
		query.setCacheable(true);
		query.setFirstResult(pageResult.getFirstRec());
		query.setMaxResults(pageResult.getPageSize());
		List ret = query.list();
		pageResult.setList(ret);

		String queryString = "";
		if (hql.toUpperCase().indexOf("SELECT") != -1) {
			int i = query.getQueryString().toUpperCase().indexOf("FROM");
			queryString = "Select count(*) " + hql.substring(i,hql.length());
		} else {
			queryString = "Select count(*) " + hql;
		}
		
		int j = queryString.toUpperCase().lastIndexOf("ORDER");
		if (j!=-1){
			queryString = queryString.substring(0, j);
		}		
		Query cquery = this.getSession().createQuery(queryString);
		cquery.setCacheable(true);
		//System.out.println(cquery.iterate().next().toString());
		int recTotal =Integer.parseInt(cquery.iterate().next().toString());
		//int recTotal = ((Integer)cquery.iterate().next()).intValue();		
		pageResult.setRecTotal(recTotal);*/
	}
}
